//
//  FamilyManageVC.m
//  AILock
//
//  Created by 谭亚明 on 2020/6/28.
//  Copyright © 2020 tym. All rights reserved.
//

#import "FamilyUserVC.h"
#import "MyFamilyTableVCell.h"
#import "Sizes.h"
#import "LYEmptyViewHeader.h"
#import "OpenlimitsVC.h"
#import "FamilyUserCell.h"
#import "AddUserVC.h"
#import "UIButton+ImageTitleSpacing.h"
#import "FamilyDetailVC.h"
#import "FamilyOptionView.h"
#import "FamilyDescVC.h"
#import "ChangeFamilyVC.h"
#import "UserTool.h"
#import "UserModel.h"
#import "FamilyService.h"
#import "FamilyNameVC.h"
#import "FamilyDetailVC.h"
#import "BlacklistVC.h"
#import "FamilymemberVC.h"
#import "Colors.h"
#import "EquipmentService.h"
#import "WHToast.h"
#import "Fonts.h"
#import "TYMButton.h"
#import "TYMBaseTableViewCell.h"
#import "FamilyManageTwoCell.h"
#import "FamilyManageOneCell.h"
#import "GroupShadowTableView.h"
#import "FamilyManageThreeCell.h"
@interface FamilyUserVC ()<GroupShadowTableViewDelegate, GroupShadowTableViewDataSource>

@property (nonatomic, strong) GroupShadowTableView *tableView;

@property(nonatomic,strong) NSMutableArray *dataArray;

@property(nonatomic,weak) IBOutlet UIButton *addUserBtn;

@property(nonatomic,strong) EquipmentService  *equipService;

@property(nonatomic,strong) EquipmentModel *equipMode;
@end

@implementation FamilyUserVC

-(instancetype)initWithEquipment:(EquipmentModel*)equipModel{
    self = [super init];
    if (self) {
        self.equipMode = equipModel;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTable];
    self.tableView.ly_emptyView = [TYMEmptyView emptyViewWithImageStr:@"empty_img"
     titleStr:@"这个门锁还没有家庭用户哦"
    detailStr:@""];
    if([UserTool getCurrentFamily].familyRole == 2){
        self.addUserBtn.hidden = YES;
    }
}

-(void)zj_viewWillAppearForIndex:(NSInteger)index{
    [self getDataArrayWithWeb];
}

- (void)setTable {
    self.tableView = [[GroupShadowTableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-NavigateonHeight - TabbarHeight*2.5) style:UITableViewStyleGrouped];
    self.tableView.sectionHeaderHeight = 0;
    self.tableView.sectionFooterHeight = 0;
    self.tableView.groupShadowDelegate = self;
    _tableView.groupShadowDataSource = self;
    _tableView.showSeparator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = viewBGColor;
    
    self.tableView.isShowArray = @[@"1"];
    [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MyFamilyTableVCell  class]) bundle:nil] forCellReuseIdentifier:[MyFamilyTableVCell identifier]];
    [self.view addSubview:_tableView];
    
    [self.addUserBtn setImage:[UIImage imageNamed:@"icon_home5"] forState:UIControlStateNormal];
    [self.addUserBtn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleLeft imageTitleSpace:3];
    self.addUserBtn.layer.borderColor = rgba(151, 151, 151).CGColor;
    self.addUserBtn.layer.cornerRadius = 5;
    self.addUserBtn.layer.borderWidth = 1;
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 0.001)]; view.backgroundColor = [UIColor redColor]; self.tableView.tableHeaderView = view;
}

-(void)backViewFunc:(id)btn{
    [super backViewFunc:btn];
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}

-(EquipmentService*)equipService{
    if (_equipService == nil) {
        _equipService = [[EquipmentService alloc] init];
    }
    return _equipService;
}

-(NSMutableArray*)dataArray{
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

-(void)getDataArrayWithWeb{
    TYMWeak(self);
    [self.equipService getEquipmentuseruserlistEquipment:_equipMode.equipmentId type:0 completion:^(LMJBaseResponse * _Nonnull respons, NSArray<UserModel *> * _Nonnull array) {
        if(weakself.dataArray.count){
            [weakself.dataArray removeAllObjects];
        }
        if(array.count){
            weakself.tableView.headTitles = @[@"门锁用户"];
             self.tableView.isShowArray = @[@"1"];
            [weakself.dataArray addObjectsFromArray:array];
            [self.tableView reloadData];
        }else{
            self.tableView.isShowArray = @[@"0"];
            [self.tableView reloadData];
            [weakself.tableView ly_showEmptyView];
        }
        
    }];

}

-(IBAction)addUserAction:(id)sender{
    AddUserVC *addVC = [[AddUserVC alloc] initWithEquipMent:self.equipMode];
    [self.navigationController pushViewController:addVC animated:YES];
}

#pragma mark delegate datasource
- (NSInteger)numberOfSectionsInGroupShadowTableView:(GroupShadowTableView *)tableView {
    return 1;
}

- (NSInteger)groupShadowTableView:(GroupShadowTableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (CGFloat)groupShadowTableView:(GroupShadowTableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [FamilyUserCell cellForHeight];
}

- (UITableViewCell *)groupShadowTableView:(GroupShadowTableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    FamilyUserCell *cell = [tableView dequeueReusableCellWithIdentifier:[FamilyUserCell identifier]];
    if (cell==nil) {
        cell = [[FamilyUserCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[FamilyUserCell identifier]];
    }
    UserModel *model = self.dataArray[indexPath.row];
    cell.userModel = model;
    cell.typeLab.text = [model getTypeStr];
    if ([UserTool getCurrentFamily].familyRole == 2) {
        [cell rightImageHidden];
    }
    return cell;
}

-(void)groupShadowTableView:(GroupShadowTableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.dataArray.count &&
        [UserTool getCurrentFamily].familyRole == 1) {
        
        OpenlimitsVC *limitsVC = [[OpenlimitsVC alloc] initWithUserModel:self.dataArray[indexPath.row] equipMent:self.equipMode];
        
        [self.navigationController pushViewController:limitsVC animated:YES];
    }
}

@end
